const path = require('path')
const os = require('os');
const fs = require('fs');
const { merge } = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

var webpack = require('webpack')

const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

config = {
    entry: {
        data: path.join(__dirname, 'src/template/entry/data.js'),
        index: path.join(__dirname, 'src/template/entry/index.js')
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'js/[name].js?[hash:5]',
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            // filename: 'index.html',
            template: './src/template/page/index.ejs',
            minify: {
                collapseWhitespace: false, //打包后是否删除空格(压缩)
                removeAttributeQuotes: false, // 移除属性的引号
            },
        }),
        new MiniCssExtractPlugin({
            filename: 'css/style.css?[hash:5]',
        }),

    ],

    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                    },
                    'css-loader',
                ],
            },
            {
                test: /\.(png|jpg|gif|svg)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash:8]',// css引用路径名称
                    outputPath: 'images/', // 输出的图片会生成并放在当前设置的文件夹下
                    publicPath: '../images/' //
                }
            },
            {
                loader: 'image-webpack-loader',
                options: {
                    mozjpeg: {
                        progressive: true,
                        quality: 90
                    },
                    optipng: {
                        enabled: false,
                    },
                    pngquant: {
                        quality: [0.65, 0.9],
                        speed: 4
                    },
                    gifsicle: {
                        interlaced: false,
                    },
                    bypassOnDebug: true
                }
            },
            {
                test: /\.(ejs)$/,
                use: [{
                    loader: 'ejs-loader',
                    options: {
                        esModule: false
                    }
                }]
            }

        ]
    },
}

module.exports = config
